package com.syyo.gulimall.order.config.rabbit;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

/**
 * @ author: wang
 * @ date: 2021/4/22 11:29
 * @ Description:
 */
@Configuration
public class MyMQConfig {


    public static final String ORDER_EXCHANGE = "order-event-exchange";// 死性路由
    public static final Integer EXPIRATION_TIME = 60000;//过期时间

    public static final String ORDER_DELAY_QUEUE = "order.delay.queue";// 延时队列
    public static final String ORDER_RELEASE_ORDER_QUEUE = "order.release.order.queue";// 释放队列
    public static final String STOCK_RELEASE_QUEUE = "stock.release.stock.queue";// 库存的释放队列
    public static final String ORDER_SECKILL_QUEUE = "order.seckill.order.queue";//秒杀队列

    public static final String ORDER_CREATE_ORDER = "order.create.order";// 释放队列绑定key
    public static final String ORDER_RELEASE_KEY = "order.release.order";//释放key
    public static final String ORDER_MATCH_KEY = "order.release.other.#";// 订单的模糊匹配key
    public static final String ORDER_MATCH_OTHER = "order.release.other";// 订单的匹配key

    public static final String ORDER_SECKILL_KEY = "order.seckill.order";//秒杀的key


    /**
     * 创建 死性队列/延迟队列
     *
     * @return
     */
    @Bean
    public Queue orderDelayQueue() {
        Map<String, Object> map = new HashMap<>();
        /*
         * x-dead-letter-exchange: order-event-exchange
         * x-dead-letter-routing-key: order.release.key
         * x-message-ttl: 60000
         */
        map.put("x-dead-letter-exchange", ORDER_EXCHANGE);
        map.put("x-dead-letter-routing-key", ORDER_RELEASE_KEY);
        map.put("x-message-ttl", EXPIRATION_TIME);
        Queue queue = new Queue(ORDER_DELAY_QUEUE, true, false, false, map);
        return queue;
    }

    /**
     * 释放订单队列
     *
     * @return
     */
    @Bean
    public Queue orderReleaseQueue() {
        Queue queue = new Queue(ORDER_RELEASE_ORDER_QUEUE, true, false, false);
        return queue;
    }

    /**
     * 注入交换机
     *
     * @return
     */
    @Bean
    public Exchange orderEventExchange() {
        return new TopicExchange(ORDER_EXCHANGE, true, false);
    }

    /**
     * 绑定订单的延迟队列
     * <p>
     * // String destination,目的地
     * // DestinationType destinationType, 目的地类型
     * // String exchange, 交换机
     * // String routingKey, 绑定key
     * // Map<String, Object> arguments 扩展参数
     */
    @Bean
    public Binding orderCreateBinding() {
        return new Binding(ORDER_DELAY_QUEUE,
                Binding.DestinationType.QUEUE,
                ORDER_EXCHANGE,
                ORDER_CREATE_ORDER,
                null);
    }

    /**
     * 绑定订单的释放队列
     *
     * @return
     */
    @Bean
    public Binding orderReleaseOrderBinding() {
        return new Binding(ORDER_RELEASE_ORDER_QUEUE,
                Binding.DestinationType.QUEUE,
                ORDER_EXCHANGE,
                ORDER_RELEASE_KEY,
                null);
    }

    /**
     * 订单释放直接和库存释放绑定
     * 绑定库存的释放队列
     *
     * @return
     */
    @Bean
    public Binding orderReleaseStockBinding() {
        return new Binding(STOCK_RELEASE_QUEUE,
                Binding.DestinationType.QUEUE,
                ORDER_EXCHANGE,
                ORDER_MATCH_KEY,
                null);
    }

    /**
     * 秒杀队列
     *
     * @return
     */
    @Bean
    public Queue orderSeckillQueue() {
        Queue queue = new Queue(ORDER_SECKILL_QUEUE, true, false, false);
        return queue;
    }

    /**
     * 订单释放直接和库存释放绑定
     * 绑定库存的释放队列
     *
     * @return
     */
    @Bean
    public Binding orderSeckillBinding() {
        return new Binding(ORDER_SECKILL_QUEUE,
                Binding.DestinationType.QUEUE,
                ORDER_EXCHANGE,
                ORDER_SECKILL_KEY,
                null);
    }
}
